package jbl.service.impl;

import java.util.ArrayList;
import java.util.List;

import jbl.dao.ZtDaoI;
import jbl.model.Code_zt;
import jbl.model.Tuser;
import jbl.pageModel.DataGrid;
import jbl.pageModel.Zt;
import jbl.service.ZtServiceI;
import jbl.util.Encrypt;
import jbl.util.JblTool;
import jbl.util.Json;

import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/*
 * 用户权限菜单表
 * */
@Service("ztService")
public class ZtServiceImpl implements ZtServiceI{
	
	private static final Logger logger = Logger.getLogger(ZtServiceImpl.class);

	
	private ZtDaoI ztDao;
	
	@Autowired
	public void setZtDao(ZtDaoI ztDao) {
		this.ztDao = ztDao;
	}


	@Override
	public DataGrid dataGrid() {
		DataGrid dg = new DataGrid();
		try {
			List<Zt> rl = new ArrayList<Zt>();
			String hql = "from Code_zt t ";
			List<Code_zt> nl = ztDao.find(hql);
			if(null != nl && nl.size() > 0){
				for(Code_zt t : nl){
					Zt zt = new Zt();
					BeanUtils.copyProperties(t, zt);
					addWj(t,zt);
					rl.add(zt);
				}
				dg.setTotal((long)rl.size());
				dg.setRows(rl);
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("查询zt表错误:"+e.getMessage());
		}
		return dg;
	}
	
	private void addWj(Code_zt t , Zt zt){
		Code_zt zts = t.getZts();
		if(zts != null){
			List<Object> list = new ArrayList<Object>();
			list.add(zts.getZt());
			list.add(zts.getId());
			zt.setZtList(list);
		}
	}
	
	@Override
	public Json add(Zt zt){
		Json j = new Json();
		try {
			Code_zt code_zt = new Code_zt();
			
			String userid = Encrypt.createUUID();
			code_zt.setId(userid);
			code_zt.setZt(zt.getZt());
			code_zt.setFlag(zt.getFlag());
			code_zt.setFlag1(zt.getFlag1());
			if(null != zt.getZtList() && !"".equals(zt.getZtList().get(0).toString())){
				Code_zt zt1 = new Code_zt();
				zt1.setId(zt.getZtList().get(0).toString());
				code_zt.setZts(zt1);
			}

			ztDao.save(code_zt);
			j.setMsg("添加成功");
			j.setSuccess(true);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			j.setMsg("添加失败");
		}
		return j;
	}
	
	@Override
	public Json edit(Zt zt){
		Json j = new Json();
		try {
			Code_zt code_zt = new Code_zt();
			BeanUtils.copyProperties(zt, code_zt);
			if(null != zt.getZtList() && !"".equals(zt.getZtList().get(0).toString())){
				Code_zt zt1 = new Code_zt();
				zt1.setId(zt.getZtList().get(0).toString());
				code_zt.setZts(zt1);
			}
			ztDao.update(code_zt);
			j.setMsg("修改成功");
			j.setSuccess(true);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			j.setMsg("修改失败");
		}
		return j;
	}
	

	@Override
	public Json delete(String  ids) {
		Json j = new Json();
		try {
			if(ids != null){
				for (String id : ids.split(",")) {
					if(!id.trim().equals("")){
						Code_zt t = ztDao.get(Code_zt.class,id.trim());
						if(t.getId().equals("1") || t.getId().equals("2") || t.getId().equals("3") || t.getId().equals("4") || t.getId().equals("5") || t.getId().equals("6") || t.getId().equals("7") || t.getId().equals("8")){
							j.setMsg(t.getZt()+"为系统内置状态,不可删除!");
							
							return j;
						}else{
							ztDao.delete(t);
						}
					}
				}
			}
			j.setMsg("删除成功");
			j.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
			j.setMsg("删除失败");
		}
		return j;
	}
	@Override
	public List<Zt> comboboxInfo() {
		List<Zt> rl = new ArrayList<Zt>();
		Tuser tuser = JblTool.getTuser();
		try {
			String hql = "from Code_zt t where 1=1 ";
			if(null == tuser){
				return null;
			}else if(!tuser.getTuserGroup().getId().equals("7a0aaea7eac94b44bde3a2b9707dc8fc")){
				hql += " and t.flag = 'T' ";
			}
			List<Code_zt> nl  = ztDao.find(hql);
			if(null != nl && nl.size() > 0){
				for(Code_zt t : nl){
					Zt zt = new Zt();
					BeanUtils.copyProperties(t, zt);
					rl.add(zt);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("查询zt表错误:"+e.getMessage());
		}
		return rl;
	}

}











